home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / CLEVENT.CPP < prev    next >
C/C++ Source or Header  |  1993-07-06  |  11KB  |  353 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    clevent.cpp
  5. //   Title:    C++ Class Libraries
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class CL_EVENT.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <class.hpp>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //   Description:    Default constructor
  45. //    Parameters:
  46. //       Returns:    
  47. //----------------------------------------------------------------------------
  48. FN_M CL_EVENT::CL_EVENT()
  49. {
  50.     CL_EVENT::Initialize(CL_INIT_CLASS);
  51. }
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Copy constructor
  56. //    Parameters:    rccl_event        Reference to object to copy.
  57. //       Returns:    
  58. //----------------------------------------------------------------------------
  59. FN_M CL_EVENT::CL_EVENT(RCCL_EVENT rccl_event)
  60. {
  61.     CL_EVENT::Initialize(CL_INIT_CLASS);
  62.     *this = rccl_event;
  63. }
  64.  
  65.  
  66. //----------------------------------------------------------------------------
  67. //   Description:    Destructor
  68. //    Parameters:
  69. //       Returns:    
  70. //----------------------------------------------------------------------------
  71. FN_M CL_EVENT::~CL_EVENT()
  72. {
  73.     CL_EVENT::Destroy(FALSE);
  74. }
  75.  
  76.  
  77. //----------------------------------------------------------------------------
  78. //   Description:    Destroy object. Free any resources used by object.
  79. //                          Normally called by destructor.
  80. //                        Should allow multiple calls from various classes.
  81. //    Parameters:    fDestroyAll        Destroy parents also?
  82. //                                            Default is TRUE.
  83. //       Returns:    TRUE if successful.
  84. //----------------------------------------------------------------------------
  85. BOOL FN_M CL_EVENT::Destroy(BOOL fDestroyAll)
  86. {
  87. #if OS_OS2
  88.     if (fCreated)
  89.         {
  90.         APIRET rc;
  91.         if ((rc = DosCloseEventSem(hev)) != 0)
  92.             Error("Problem destroying semaphore (%ld)", (LONG)rc);
  93.         fCreated = FALSE;
  94.         }
  95. #endif
  96.     CL_EVENT::Initialize(CL_INIT_CLASS_VARS);
  97.     if (fDestroyAll)                            // Destroy parent.
  98.         CL_EVENT_PARENT::Destroy(fDestroyAll);                    
  99.     return TRUE;
  100. }
  101.  
  102.  
  103. //----------------------------------------------------------------------------
  104. //   Description:    Initialize object. 
  105. //                          Normally called by constructor.
  106. //                        Should allow multiple calls from various classes.
  107. //    Parameters:    sInit        Initialization code. May be one of the following:
  108. //                                        CL_INIT_CLASS            Reset class variables and
  109. //                                                                    and dynamic allocations for
  110. //                                                                    this class only.
  111. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  112. //                                                                    this class only.
  113. //                                        CL_INIT_VARS            Reset class variables for
  114. //                                                                    this class only.
  115. //                                        CL_INIT_ALL                Initialize class and all 
  116. //                                                                    parent class, including
  117. //                                                                    dynamic memory allocation.
  118. //                                    Default is CL_INIT_ALL
  119. //       Returns:    TRUE if successful.
  120. //----------------------------------------------------------------------------
  121. BOOL FN_M CL_EVENT::Initialize(SHORT sInit)
  122. {
  123.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  124.         CL_EVENT_PARENT::Initialize(sInit);
  125.  
  126. #if OS_OS2
  127.     fCreated = FALSE;
  128. #endif
  129.  
  130.     if (sInit == CL_INIT_CLASS_VARS || sInit == CL_INIT_VARS)
  131.         return TRUE;
  132.  
  133. #if OS_OS2
  134.     APIRET rc = DosCreateEventSem(NULL, &hev, 0, 0);
  135.     if (rc != 0)
  136.         return Error("Problem creating event semaphore (%ld)", (LONG)rc);
  137.  
  138.     fCreated = TRUE;
  139. #endif
  140.  
  141.     return TRUE;
  142. }
  143.  
  144.  
  145. //----------------------------------------------------------------------------
  146. //   Description:    Check if object is in error state.
  147. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  148. //    Parameters:
  149. //       Returns:    TRUE if in error state.
  150. //----------------------------------------------------------------------------
  151. BOOL FN_M CL_EVENT::IsError() const
  152. {
  153.     return CL_EVENT_PARENT::IsError();
  154. }
  155.  
  156.  
  157. //----------------------------------------------------------------------------
  158. //   Description:    Check if object is valid
  159. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  160. //    Parameters:
  161. //       Returns:    TRUE if valid
  162. //----------------------------------------------------------------------------
  163. BOOL FN_M CL_EVENT::IsValid() const
  164. {
  165.     return CL_EVENT_PARENT::IsValid();
  166. }
  167.  
  168.  
  169. //----------------------------------------------------------------------------
  170. //   Description:    Assignment operator
  171. //                          NOTE: Don't copy object into self
  172. //    Parameters:    rccl_event        Reference to right value.
  173. //       Returns:    Reference to new object.
  174. //----------------------------------------------------------------------------
  175. RCCL_EVENT FN_M CL_EVENT::operator=(RCCL_EVENT rccl_event)
  176. {
  177.     if (this != &rccl_event)
  178.         {
  179.         Invalid("CL_EVENT::operator=");
  180.         }
  181.     return (RCCL_EVENT)*this;
  182. }
  183.  
  184.  
  185. //----------------------------------------------------------------------------
  186. //   Description:    Post an event semaphore
  187. //    Parameters:    
  188. //       Returns:    TRUE if successful.
  189. //----------------------------------------------------------------------------
  190. BOOL FN_M CL_EVENT::Post()
  191. {
  192. #if OS_OS2
  193.     APIRET rc = DosPostEventSem(hev);
  194.    if (rc != 0)
  195.       return Error("Problem posting event semaphore (%ld)", (LONG)rc);
  196. #endif
  197.     return TRUE;
  198. }
  199.  
  200.  
  201. //----------------------------------------------------------------------------
  202. //   Description:    Reset an event semaphore
  203. //    Parameters:    
  204. //       Returns:    TRUE if successful.
  205. //----------------------------------------------------------------------------
  206. BOOL FN_M CL_EVENT::Reset()
  207. {
  208. #if OS_OS2
  209.      ULONG ulPostCt;   
  210.     APIRET rc = DosResetEventSem(hev, &ulPostCt);
  211.    if (rc != 0)
  212.       Error("Problem resetting event semaphore (%ld)", (LONG)rc);
  213. #endif
  214.     return TRUE;
  215. }
  216.  
  217.  
  218. //----------------------------------------------------------------------------
  219. //   Description:    Retrieve object from persistent storage
  220. //    Parameters:    pcsz        Name of object.
  221. //                        pcszSub    Sub-name of object.
  222. //                                    The first character of the name should be '~'.
  223. //                                    If NULL, no sub name is available.
  224. //                                    Default is NULL
  225. //       Returns:    TRUE if successful.
  226. //----------------------------------------------------------------------------
  227. BOOL FN_M CL_EVENT::Retrieve(PCSZ pcsz, PCSZ pcszSub)
  228. {
  229.     NOTUSED(pcsz);
  230.     NOTUSED(pcszSub);
  231.     Invalid("CL_EVENT::Retrieve");
  232.     return FALSE;
  233. }
  234.  
  235.  
  236. //----------------------------------------------------------------------------
  237. //   Description:    Store object to persistent storage
  238. //    Parameters:    pcsz        Name of object.
  239. //                        pcszSub    Sub-name of object.
  240. //                                    The first character of the name should be '~'.
  241. //                                    If NULL, no sub name is available.
  242. //                                    Default is NULL
  243. //       Returns:    TRUE if successful.
  244. //----------------------------------------------------------------------------
  245. BOOL FN_M CL_EVENT::Store(PCSZ pcsz, PCSZ pcszSub)
  246. {
  247.     NOTUSED(pcsz);
  248.     NOTUSED(pcszSub);
  249.     Invalid("CL_EVENT::Store");
  250.     return FALSE;
  251. }
  252.  
  253.  
  254. //----------------------------------------------------------------------------
  255. //   Description:    Wait for an event semaphore to be posted.
  256. //    Parameters:    ulWait        Number of milliseconds to wait.
  257. //                                        If -1l, wait forever
  258. //                                        Default is -1.
  259. //       Returns:    TRUE if successful.
  260. //----------------------------------------------------------------------------
  261. BOOL FN_M CL_EVENT::Wait(ULONG ulWait)
  262. {
  263. #if OS_OS2
  264. #    ifndef ERROR_TIMEOUT
  265. #        define  ERROR_TIMEOUT    640
  266. #        define  ERROR_INTERRUPT   95
  267. #    endif
  268.  
  269.     APIRET rc = DosWaitEventSem(hev, ulWait);
  270.    if (rc == ERROR_TIMEOUT)
  271.         return TRUE;
  272.    if (rc != 0)
  273.       return Error("Problem while waiting for event semaphore (%ld)", (LONG)rc);
  274. #else
  275.     NOTUSED(ulWait);
  276. #endif
  277.     return TRUE;
  278. }
  279.  
  280.  
  281. //----------------------------------------------------------------------------
  282. //   Description:    Print object value to debugging output.
  283. //    Parameters:    pccl_event        Pointer to dynamic object. 
  284. //                                    If NULL, static data elements are printed.
  285. //                                    Default is NULL.
  286. //                        pcsz        Name of object.
  287. //                                    If NULL, no name is displayed.
  288. //                                    Default is NULL.
  289. //                        cLevel    Display level. 
  290. //                                    Default is zero.
  291. //       Returns:
  292. //----------------------------------------------------------------------------
  293. #if COMPILE_DEBUG
  294. VOID FN_M CL_EVENT::Print(PCCL_EVENT pccl_event, PCSZ pcsz, SIZET cLevel)
  295. {
  296. #if COMPILE_TEST
  297.     OutputL(cLevel, "CL_EVENT%s%s", (pcsz?"::":""), (pcsz?pcsz:""));
  298.     cLevel++;
  299.     if (pccl_event)
  300.         {
  301.         Output(" <%p>\n", pccl_event);
  302.         if(!pccl_event->IsError())
  303.             {
  304. #if OS_OS2
  305.             OutputL(cLevel, "hev = %p\n", pccl_event->hev);
  306.             OutputL(cLevel, "fCreated = %ld\n", (LONG)pccl_event->fCreated);
  307. #endif
  308.             }
  309.         }
  310.     else
  311.         Output(" <NULL>\n");
  312.     CL_EVENT_PARENT::Print((CL_EVENT_PARENT _FAR_ *)pccl_event, pcsz, cLevel);
  313.     return ;
  314. #else
  315.     NOTUSED(cLevel);
  316.     NOTUSED(pccl_event);
  317.     NOTUSED(pcsz);
  318.     return ;
  319. #endif
  320. }
  321. #endif
  322.  
  323.  
  324. //----------------------------------------------------------------------------
  325. //   Description:    Run standard test suite on object.
  326. //    Parameters:    sTest        Test to run.
  327. //                                    If 0, run default tests.
  328. //                                    Default is 0.
  329. //       Returns:    TRUE if successful.
  330. //----------------------------------------------------------------------------
  331. #if COMPILE_DEBUG
  332. BOOL FN_M CL_EVENT::Test(SHORT sTest)
  333. {
  334. #if COMPILE_TEST
  335.     if (sTest == 1)                            // Test 1 is always a test of storage
  336.         {
  337.         CL_EVENT cl_event;
  338.         cl_event.Store("CL_EVENT");
  339.         cl_event.Retrieve("CL_EVENT");
  340.         CL_EVENT::Print(&cl_event);
  341.         }
  342.     return TRUE;
  343. #else
  344.     NOTUSED(sTest);
  345.     return TRUE;
  346. #endif
  347. }
  348. #endif
  349. //----------------------------------------------------------------------------
  350. //------------------------------- End of File --------------------------------
  351. //----------------------------------------------------------------------------
  352.  
  353.